<?php
use yii\helpers\Url;
use yii\web\View;
// use yii\Web\View;
use app\assets\NewCpeCreativeAsset;
NewCpeCreativeAsset::register($this);
$this->title = $this->context->pageTitle.Yii::$app->params['titleSuffix'];
$this->params['breadcrumbs'] = [
  [
    'label' => \Yii::t('creative', 'Creative'),
    'url' => ['creative-list']
  ],
  \Yii::t('creative', 'Edit Creative')
];
$jsMsgs = $this->context->loadJsMsgs([
  'creativeForm'
]);
$this->registerJs("VAM.setVar('LANGUAGE_JS', '".$jsMsgs."');", View::POS_HEAD);
?>
<div class="page-header">
  <h1>
    <?=\Yii::t('creative', 'Creative')?>
    <small><?=\Yii::t('creative', 'Edit CPE Creative')?></small>
  </h1>
</div>
<div class="panel panel-default">
  <div class="panel-heading">
    <i class="fa fa-plus-circle"></i>
    <?=\Yii::t('creative', 'Edit CPE Creative')?>
    <ul class="list-unstyled pull-right">
      <li>
        <a href="#">
          <i class="fa fa-chevron-up"></i>
        </a>
      </li>
    </ul>
  </div>
  <div class="panel-body">
    <div class="row">
      <div class="col-sm-12">
        <div class="section-step">
          <ol class="list-inline main-flow">
            <li class="first">
              <a href="<?= Url::to(['creative/cpe-banner-creative-edit']);?>?id=<?=$creative['id']?>" class="cancleFocus">
                <b>①</b>
                <?=\Yii::t('creative', 'Setting CPE Banner')?>
                <span><i class="fa fa-arrow-right"></i></span>
              </a>
            </li>
            <li class="last current">
              <a href="<?= Url::to(['creative/cpe-video-creative-edit']);?>?id=<?=$creative['id']?>" class="cancleFocus">
                <b>②</b>
                <?=\Yii::t('creative', 'Setting CPE Video')?>
              </a>
            </li>
          </ol>
        </div>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-8 col-sm-offset-2">
        <form class="form-horizontal" id="data-frm" method="POST" action="<?= Url::to(['creative/new-cpe-video-creative-handle']);?>">
        <!-- <form class="form-horizontal" id="data-frm" method="POST" action="<?= Yii::getAlias('@web') . '/response/creative/uploadMaterialDataList.php'?>"> -->
          <input class="hidden" type="text" name="id" value="<?=$creative['id']?>">
          <fieldset>
            <legend><?=\Yii::t('creative', 'Creative Info')?></legend>
            <div class="form-group">
              <label class="col-sm-3 control-label" for="landing_page_url">
                <em>*</em>
                <?=\Yii::t('creative', 'Landing Page Url')?>：
              </label>
              <div class="col-sm-7">
                <input class="form-control" id="landing_page_url" type="text" name="landing_page" value="<?=$creative['cpe_landing_page']?>" placeholder="http(s)://">
                <div class="help-block text-error" id="landing_page_url_err"></div>
              </div>
            </div>
              <?php
        $shows = explode(';;;', $creative['cpe_show_monitor_url']);
        $title = \Yii::t('creative', 'Click to add show url');
        $showUrl = \Yii::t('creative', 'Show Url').':';
        foreach ($shows as $i => $show) {
          $btn = ' del-operate';
          $icon = 'fa-minus-circle';
          $label = '';
          $i == 0 && $btn = 'add-monitor-url';
          $i == 0 && $icon = 'fa-plus-circle';
          $i == 0 && $label = $showUrl;

          echo $showDiv = <<<FINE
            <div class="form-group impr-monitor-url monitor-url-form">
              <label class="col-sm-3 control-label" for="impr_monitor_url">
                $label
              </label>
              <div class="col-sm-7">
                <input class="form-control" type="text" data-role="show_url" name="show_monitor_url[]" value="$show" placeholder="http(s)://">
                <div class="help-block text-error monitor-url-error"></div>
              </div>
              <a class="btn btn-link $btn" title="$title" data-type="impr-monitor-url">
                <i class="fa $icon"></i>
              </a>
            </div>
FINE;
        }
      ?>
            <div class="form-group impr-monitor-url-new impr-monitor-url monitor-url-form hidden">
              <label class="col-sm-3 control-label" for="impr_monitor_url">
              </label>
              <div class="col-sm-7">
                <input class="form-control" type="text"  data-role="show_url" name="show_monitor_url[]" value="" placeholder="http(s)://">
                <div class="help-block text-error monitor-url-error"></div>
              </div>
              <a class="btn btn-link del-operate" title="<?=\Yii::t('creative', 'Click to delete show url')?>" data-type="impr-monitor-url">
                <i class="fa fa-plus-circle fa-minus-circle"></i>
              </a>
            </div>
            <?php
        $clicks = explode(';;;', $creative['cpe_click_monitor_url']);
        $title = \Yii::t('creative', 'Click to add click url');
        $showUrl = \Yii::t('creative', 'Click Url').':';
        foreach ($clicks as $i => $click) {
          $btn = ' del-operate';
          $icon = 'fa-minus-circle';
          $label = '';
          $i == 0 && $btn = 'add-monitor-url';
          $i == 0 && $icon = 'fa-plus-circle';
          $i == 0 && $label = $showUrl;

          echo $showDiv = <<<FINE
            <div class="form-group click-monitor-url monitor-url-form">
              <label class="col-sm-3 control-label" for="click_monitor_url" >
                $label
              </label>
              <div class="col-sm-7">
                <input class="form-control" data-role="click_url" type="text" name="click_monitor_url[]" value="$click" placeholder="http(s)://">
                <div class="help-block text-error monitor-url-error"></div>
              </div>
              <a class="btn btn-link $btn" title="$title" data-type="click-monitor-url">
                <i class="fa $icon"></i>
              </a>
            </div>
FINE;
        }
      ?>
            <div class="form-group click-monitor-url-new click-monitor-url monitor-url-form hidden">
              <label class="col-sm-3 control-label" for="click_monitor_url" >
              </label>
              <div class="col-sm-7">
                <input class="form-control" data-role="click_url" type="text" name="click_monitor_url[]" value="" placeholder="http(s)://">
                <div class="help-block text-error monitor-url-error"></div>
              </div>
              <a class="btn btn-link del-operate" title="<?=\Yii::t('creative', 'Click to delete click url')?>" data-type="click-monitor-url">
                <i class="fa fa-plus-circle fa-minus-circle"></i>
              </a>
            </div>
            
            <!-- visible urls -->
            <?php
        $visibleUrls = explode('###vamaker-seperator###', $creative['visible_urls']);
        $title = \Yii::t('creative', 'Click to add visible url');
        $showUrl = \Yii::t('creative', 'Visible Url').':';
        foreach ($visibleUrls as $i => $url) {
          $btn = ' del-operate';
          $icon = 'fa-minus-circle';
          $label = '';
          $i == 0 && $btn = 'add-monitor-url';
          $i == 0 && $icon = 'fa-plus-circle';
          $i == 0 && $label = $showUrl;

          echo $showDiv = <<<FINE
            <div class="form-group click-visible-url visible-url-form">
              <label class="col-sm-3 control-label" for="visible_url" >
                $label
              </label>
              <div class="col-sm-7">
                <input class="form-control" data-role="visible_url" type="text" name="visible_urls[]" value="$url" placeholder="">
                <div class="help-block text-error monitor-url-error"></div>
              </div>
              <a class="btn btn-link $btn" title="$title" data-type="click-visible-url">
                <i class="fa $icon"></i>
              </a>
            </div>
FINE;
        }
      ?>
            <div class="form-group click-visible-url-new click-visible-url visible-url-form hidden">
              <label class="col-sm-3 control-label" for="visible_url" >
              </label>
              <div class="col-sm-7">
                <input class="form-control" data-role="visible_url" type="text" name="visible_urls[]" value="" placeholder="">
                <div class="help-block text-error visible-url-error"></div>
              </div>
              <a class="btn btn-link del-operate" title="<?=\Yii::t('creative', 'Click to delete visible url')?>" data-type="click-visible-url">
                <i style="color: red" class="fa fa-plus-circle fa-minus-circle"></i>
              </a>
            </div>
            <!-- end of visible urls -->

            <div class="form-group">
              <label class="col-sm-3 control-label" for="audit_target_url">
                <?=\Yii::t('creative', 'Audit Url')?>：
              </label>
              <div class="col-sm-7">
                <input class="form-control" id="audit_target_url" type="text" name="adomain" value="<?=$creative['adomain_full']?>" placeholder="http(s)://">
                <div class="help-block text-error" id="audit_target_url_err"></div>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label" for="alternative_creative_size">
                <?=\Yii::t('creative', 'Valid Size')?>：
              </label>
              <div class="col-sm-7">
                <p class="form-control-static" id="alternative_creative_size" name="alternative_creative_size">
                640*480
                </p>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label" for="alternative_creative_size">
                <?=\Yii::t('creative', 'Valid Material Type')?>：
              </label>
              <div class="col-sm-7">
                <p class="form-control-static" id="alternative_creative_size" name="alternative_creative_size">MP4,OGG,WEBM</p>
              </div>
            </div>
            <div class="form-group" id="material_source">
              <label class="col-sm-3 control-label" for="alternative_creative_size">
                <?=\Yii::t('creative', 'Material Source')?>：
              </label>
              <div class="col-sm-7">
                <label class="radio-inline">
                  <input type="radio" name="material_source" value="0" <?if($creative['cpe_material_source'] == 0) echo "checked";?>><?=\Yii::t('creative', 'Upload Material')?>
                </label>
                <label class="radio-inline">
                  <input type="radio" name="material_source" value="1" <?if($creative['cpe_material_source'] == 1) echo "checked";?>><?=\Yii::t('creative', 'Custom Url')?>
                </label>
              </div>
            </div>
            <div id="custom_material_url">
                   <?php
                  if($creative['cpe_material_source'] == 1){
                    $cpe_size_lists = explode(',',$creative['cpe_size_list']);
                    $cpe_file_urls = explode(',',$creative['cpe_file_url']);
                    $cpe_exts = explode(',',$creative['cpe_ext']);
                    $cpe_durations = explode(',',$creative['cpe_duration']);
                    foreach ($cpe_file_urls as $key => $value) {
                  ?>
                <div class="custom-material-url">
                  <div class="form-group">
                  <label for="custom-url" class="control-label col-sm-3 required">
                    <em>*</em>
                    <?=\Yii::t('creative', 'Material Url')?>：
                  </label>
                  <div class="col-sm-7">
                    <input type="text" id="" class="form-control custom-url" name="custom_url[]" value="<?php echo $value;?>" data-err="custom-url-err">
                    <div class="help-block text-error custom-url-err"></div>
                  </div>
                  <?php
                  if($key==0){
                  ?>
                  <a class="btn btn-link add-material-url" title="<?=\Yii::t('creative', 'Click to add material url')?>" data-type="custom-material-url">
                    <i class="fa fa-plus-circle"></i>
                  </a>
                  <?php }else{ ?>
                  <a class="btn btn-link del-operate" title="<?=\Yii::t('creative', 'Click to delete material url')?>" data-type="custom-material-url">
                    <i class="fa fa-minus-circle"></i>
                  </a>
                  <?php } ?>   
                </div>
                <div class="form-group">
                  <label class="col-sm-3 control-label" for="alternative_creative_size">
                    <?=\Yii::t('creative', 'Material Size')?>：
                  </label>
                  <div class="col-sm-7">
                    <select name="material_size[]" id="" class="form-control">
                      <option value="<?php echo $cpe_size_lists[$key];?>">640*480</option>
                    </select>
                  </div>
                </div>
                <div class="form-group">
                  <label class="col-sm-3 control-label" for="" >
                    <em>*</em>
                    <?=\Yii::t('creative', 'Duration')?>：
                  </label>
                  <div class="col-sm-7">
                    <input class="form-control" type="text" name="duration[]" value="<?php echo $cpe_durations[$key];?>" data-err="duration-error">
                    <div class="help-block text-error duration-error"></div>
                  </div>
                </div>
                <div class="form-group">
                  <label for="material-type" class="col-md-3 control-label required">
                    <em>*</em>
                    <?=\Yii::t('creative', 'Material Type')?>：
                  </label>
                  <div class="col-md-7">
                    <select name="material_type[]" id="" class="form-control">
                      <option value="MP4" <?if (strtoupper($cpe_exts[$key]) == 'MP4') echo "selected";?>>MP4</option>
                      <option value="OGG" <?if (strtoupper($cpe_exts[$key]) == 'OGG') echo "selected";?>>OGG</option>
                      <option value="WEBM" <?if (strtoupper($cpe_exts[$key]) == 'WEBM') echo "selected";?>>WEBM</option>
                    </select>
                  </div>
                </div>
                </div>
                <?php
                  }
                }else{
                ?>
                <div class="custom-material-url">
                  <div class="form-group">
                    <label for="custom-url" class="control-label col-sm-3 required">
                      <em>*</em>
                      <?=\Yii::t('creative', 'Material Url')?>：
                    </label>
                    <div class="col-sm-7">
                      <input type="text" id="" class="form-control custom-url" name="custom_url[]" value="" data-err="custom-url-err">
                      <div class="help-block text-error custom-url-err"></div>
                    </div>
                    <a class="btn btn-link add-material-url" title="<?=\Yii::t('creative', 'Click to add material url')?>" data-type="custom-material-url">
                      <i class="fa fa-plus-circle"></i>
                    </a>
                  </div>
                  <div class="form-group">
                    <label class="col-sm-3 control-label" for="alternative_creative_size">
                      <em>*</em>
                      <?=\Yii::t('creative', 'Material Size')?>：
                    </label>
                    <div class="col-sm-7">
                      <select name="material_size[]" id="" class="form-control">
                        <option value="640*480">640*480</option>
                      </select>
                    </div>
                  </div>
                  <div class="form-group">
                    <label class="col-sm-3 control-label" for="" >
                      <em>*</em>
                      <?=\Yii::t('creative', 'Duration')?>：
                    </label>
                    <div class="col-sm-7">
                      <input class="form-control" type="text" name="duration[]" value="" data-err="duration-error">
                      <div class="help-block text-error duration-error"></div>
                    </div>
                  </div>
                  <div class="form-group">
                    <label for="material-type" class="col-md-3 control-label required">
                      <em>*</em>
                      <?=\Yii::t('creative', 'Material Type')?>：
                    </label>
                    <div class="col-md-7">
                      <select name="material_type[]" id="" class="form-control">
                        <option value="MP4">MP4</option>
                        <option value="OGG">OGG</option>
                        <option value="WEBM">WEBM</option>
                      </select>
                    </div>
                  </div>
                </div>
                <?php }?>
              <div class="help-block text-error" id="custom_material_url_err"></div>
            </div>
            <div id="local_upload_material">
              <div class="form-group">
                <label class="col-sm-3 control-label required" id="upload_material_label" for="upload_material">
                  <?=\Yii::t('creative', 'Upload Material')?>：
                </label>
                <div class="col-sm-2 col-sm-offset-6">
                  <button class="btn btn-info" id="upload-material" type="button">
                    <i class="fa fa-upload"></i>
                    <?=\Yii::t('creative', 'Upload')?>
                  </button>
                </div>
                <div class="col-sm-12 mt10 text-center">
                  <div class="creative-preview-wrap">    
                      <?php
                         if($creative['cpe_material_source'] == 0 && !empty($creative['cpe_material_id'])){
                          $cpe_material_ids = explode(',',$creative['cpe_material_id']);
                          $cpe_size_lists = explode(',',$creative['cpe_size_list']);
                          $cpe_file_urls = explode(',',$creative['cpe_file_url']);
                          $cpe_exts = explode(',',$creative['cpe_ext']);
                          foreach ($cpe_material_ids as $key => $value) {
                            $id = $value;
                            $path = $cpe_file_urls[$key];
                            $ext = $cpe_exts[$key];
                            $sizelist = explode('*', $cpe_size_lists[$key]);
                            $width = $sizelist[0];
                            $height = $sizelist[1];
                            $delete = \Yii::t('creative', 'Delete');
                            $preview = <<<FINE
                              <div class="creative-preview">
                              <div class="flowplayer">
                                <video width="${width}" height="${height}">
                                  <source src="${path}" type="video/${ext}">
                                </video>       
                              </div>
                              <input id="upload_material" data-err="upload_material_err" type="hidden" name="" value="${id}">
                              <input type="hidden" name="upload_material[]" value="${id}">
                              <div class="help-block text-error" id="upload_material_err"></div>
                              <button class="btn btn-link btn-s btn-sm preview-close">$delete</button>
                              </div>               
FINE;
                          echo $preview;
                          }
                          }else{
                         echo '';
                         }
                      ?>    
                  </div>
                  <img class="img-thumbnail creative_material_default <?php if($creative['cpe_material_source'] == 0 && !empty($creative['cpe_material_id'])) echo "hidden";?>" id="creative_material_default" data-src="holder.js/310x260/text:<?=\Yii::t('creative', 'Not upload')?>" alt="<?=\Yii::t('creative', 'Not upload')?>">
                </div>
              </div>
            </div>
          </fieldset>
          <fieldset>
            <div class="form-group">
              <div class="col-sm-offset-3 col-sm-7">
                <div class="btn-form-group pull-right">
                  <input class="operate-type" type="hidden" name="operate_type" value="save">
                  <button type="submit" class="btn btn-primary" data-loading-text="<i class='fa fa-spinner fa-spin'></i> <?=\Yii::t('common', 'Saving')?>..."><?=\Yii::t('common', 'Save')?></button>
                  <a href="<?php echo Url::to(['creative/creative-list']); ?>" class="btn btn-default"><?=\Yii::t('common', 'Back')?></a>
                </div>
              </div>
            </div>
          </fieldset>
        </form>
      </div>
    </div>
  </div>
</div>
<div class="modal fade" id="upload-material-modal">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <h4 class="modal-title"><?=\Yii::t('creative', 'Upload Material')?></h4>
      </div>
      <div class="modal-body">
        <div class="portlet-content">
          <span class="btn btn-success btn-sm fileinput-button">
            <i class="fa fa-plus"></i>
            <span><?=\Yii::t('creative', 'File')?></span>
            <input id="creative-id" type="hidden" name="creative_id" value="${id}" >
            <!-- The file input field used as target for the file upload widget -->
            <input type="file" multiple="" name="files[]" id="fileupload">
          </span>
          <br>
          <br>
          <!-- The global progress bar -->
          <div class="progress" id="progress">
              <div aria-valuemax="100" aria-valuemin="0" class="progress-bar progress-bar-success progress-bar-striped">0%</div>
          </div>
          <!-- The container for the uploaded files -->
          <div class="files" id="files">
            <table class="table table-striped table-bordered">
              <thead>
                <tr>
                  <th>ID</th>
                  <th><?=\Yii::t('creative', 'File Name')?></th>
                  <th><?=\Yii::t('creative', 'File Size')?>(KB)</th>
                  <th><?=\Yii::t('creative', 'Upload Time')?></th>
                  <th><?=\Yii::t('creative', 'Info')?></th>
                  <th><?=\Yii::t('creative', 'Uploader')?></th>
                </tr>
              </thead>
              <tbody></tbody>
              </table>
            </div>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal"><?=\Yii::t('common', 'Close')?></button>
        <button type="button" class="btn btn-primary" id="upload-confirm-operate" data-material-id=""><?=\Yii::t('common', 'Sure')?></button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<div class="custom-material-url new-custom-url hidden">
  <div class="form-group">
    <label for="custom-url" class="control-label col-sm-3">
      <?=\Yii::t('creative', 'Material Url')?>：
    </label>
    <div class="col-sm-7">
      <input type="text" id="" class="form-control custom-url" name="custom_url[]" value="" data-err="custom-url-err">
      <div class="help-block text-error custom-url-err"></div>
    </div>
    <a class="btn btn-link del-operate" title="<?=\Yii::t('creative', 'Click to delete material url')?>" data-type="custom-material-url">
      <i class="fa fa-plus-circle fa-minus-circle"></i>
    </a>
  </div>
  <div class="form-group">
    <label class="col-sm-3 control-label" for="alternative_creative_size">
      <?=\Yii::t('creative', 'Material Source')?>：
    </label>
    <div class="col-sm-7">
      <select name="material_size[]" id="" class="form-control">
        <option value="640*480">640*480</option>
      </select>
    </div>
  </div>
  <div class="form-group">
    <label class="col-sm-3 control-label" for="" >
      <em>*</em>
      <?=\Yii::t('creative', 'Duration')?>：
    </label>
    <div class="col-sm-7">
      <input class="form-control" type="text" name="duration[]" value="" data-err="duration-error">
      <div class="help-block text-error duration-error"></div>
    </div>
  </div>
  <div class="form-group">
    <label for="material-type" class="col-md-3 control-label required">
      <em>*</em>
      <?=\Yii::t('creative', 'Material Type')?>：
    </label>
    <div class="col-md-7">
      <select name="material_type[]" id="" class="form-control">
        <option value="MP4">MP4</option>
        <option value="OGG">OGG</option>
        <option value="WEBM">WEBM</option>
      </select>
    </div>
  </div>
</div>
<?php
  $uploadMaterialUrl =  Url::to(['creative/upload-video']);
  $creativeVocation = $categories;
  $nextUrl = Url::to(['creative/new-cpe-video-creative']);
  $videoPluginUrl = Yii::getAlias('@web/').Yii::$app->params['envDir'].'/js/vendor/flowplayer/flowplayer.swf';
  $this->registerJs("VAM.setVar('UPLOAD_MATERIAL_URL','$uploadMaterialUrl');", View::POS_HEAD);
  $this->registerJs("VAM.setVar('CREATIVE_VOCATION','$creativeVocation')", View::POS_HEAD);
  $this->registerJs("VAM.setVar('NEXT_URL','$nextUrl')", View::POS_HEAD);
  $this->registerJs("VAM.setVar('VIDEO_PLUGIN_URL','$videoPluginUrl');", View::POS_HEAD);
?>
<script type="text/javascript" >
  requirejs(["<?= Yii::getAlias('@web/').Yii::$app->params['envDir'];?>/js/site.js"], function(common) {
    requirejs(["<?= Yii::getAlias('@web/').Yii::$app->params['envDir'];?>/js/creative/newCpeVideoCreative.js"]);
  });
</script>